﻿
namespace CSharpPro._1103;


class Program
{
   private static IUserService userService = new UserServiceImpl();
    
    static void Main()
    {
        Console.WriteLine("=== 用户管理系统（重构版） ===");
        ShowMainMenu();
    }
    
    static void ShowMainMenu()
    {
        while (true)
        {
            Console.WriteLine("\n请选择操作：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 管理用户");
            Console.WriteLine("4. 退出");
            Console.Write("请输入选项(1-4): ");
            
            string choice = Console.ReadLine()?.Trim();
            
            switch (choice)
            {
                case "1":
                    LoginProcess();
                    break;
                case "2":
                    RegisterProcess();
                    break;
                case "3":
                    ManageUsersProcess();
                    break;
                case "4":
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("请输入有效数字(1-4)！");
                    break;
            }
        }
    }
    
    static void LoginProcess()
    {
        Console.WriteLine("\n=== 用户登录 ===");
        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        Console.Write("请输入密码: ");
        string password = Console.ReadLine()?.Trim();
        
        if (userService.Login(username, password))
        {
            ShowUserHomePage(username);
        }
    }
    
    static void RegisterProcess()
    {
        Console.WriteLine("\n=== 用户注册 ===");
        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        Console.Write("请输入密码: ");
        string password = Console.ReadLine()?.Trim();
        
        Console.Write("请确认密码: ");
        string confirmPassword = Console.ReadLine()?.Trim();
        
        userService.Register(username, password, confirmPassword);
    }
    
    static void ManageUsersProcess()
    {
        Console.WriteLine("\n=== 用户管理 ===");
        Console.WriteLine("1. 查看所有用户");
        Console.WriteLine("2. 删除用户");
        Console.WriteLine("3. 返回主菜单");
        Console.Write("请选择操作: ");
        
        string choice = Console.ReadLine()?.Trim();
        
        switch (choice)
        {
            case "1":
                ShowAllUsers();
                break;
            case "2":
                DeleteUserProcess();
                break;
            case "3":
                return;
            default:
                Console.WriteLine("无效选择！");
                break;
        }
    }
    
    static void ShowAllUsers()
    {
        List<User> allUsers = userService.GetAllUsers();
        
        if (allUsers.Count == 0)
        {
            Console.WriteLine("暂无注册用户！");
            return;
        }
        
        Console.WriteLine($"\n=== 所有用户列表（共{allUsers.Count}个用户）===");
        
        // 使用foreach循环迭代显示所有用户
        int index = 1;
        foreach (User user in allUsers)
        {
            Console.WriteLine($"{index}. {user}");
            index++;
        }
    }
    
    static void DeleteUserProcess()
    {
        Console.Write("请输入要删除的用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不能为空！");
            return;
        }
        
        userService.DeleteUser(username);
    }
    
    static void ShowUserHomePage(string username)
    {
        while (true)
        {
            Console.WriteLine($"\n=== 系统首页 - 欢迎 {username} ===");
            Console.WriteLine("1. 查看个人信息");
            Console.WriteLine("2. 修改密码");
            Console.WriteLine("3. 退出登录");
            Console.Write("请选择操作: ");
            
            string choice = Console.ReadLine()?.Trim();
            
            switch (choice)
            {
                case "1":
                    ShowUserInfo(username);
                    break;
                case "2":
                    ChangePasswordProcess(username);
                    break;
                case "3":
                    Console.WriteLine("已退出登录！");
                    return;
                default:
                    Console.WriteLine("无效选择！");
                    break;
            }
        }
    }
    
    static void ShowUserInfo(string username)
    {
        User user = userService.GetUserInfo(username);
        if (user != null)
        {
            Console.WriteLine($"\n=== 个人信息 ===");
            Console.WriteLine($"用户名: {user.Username}");
            Console.WriteLine($"密码: {new string('*', user.Password.Length)}");
            Console.WriteLine($"注册时间: {user.RegisterTime:yyyy-MM-dd HH:mm:ss}");
        }
        
        Console.WriteLine("\n按任意键返回...");
        Console.ReadKey();
    }
    
    static void ChangePasswordProcess(string username)
    {
        Console.WriteLine("\n=== 修改密码 ===");
        
        Console.Write("请输入原密码: ");
        string oldPassword = Console.ReadLine()?.Trim();
        
        Console.Write("请输入新密码: ");
        string newPassword = Console.ReadLine()?.Trim();
        
        Console.Write("请确认新密码: ");
        string confirmNewPassword = Console.ReadLine()?.Trim();
        
        userService.ChangePassword(username, oldPassword, newPassword, confirmNewPassword);
    }

    

}